/*
 * Created by JFormDesigner on Sat Mar 26 16:36:15 CST 2022
 */

package com.wangcanxuan.www.view.user;

import com.wangcanxuan.www.controller.Impl.UserControllerImpl;
import com.wangcanxuan.www.controller.UserController;
import com.wangcanxuan.www.entity.dto.ResultInfo;
import com.wangcanxuan.www.entity.po.User;
import com.wangcanxuan.www.proxy.ControllerProxy;
import com.wangcanxuan.www.util.ConnectionPoolUtil;
import com.wangcanxuan.www.util.ValidateUtil;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import static com.wangcanxuan.www.entity.dto.ResultConstants.*;

/**
 * 注册界面
 *
 * @author unknown
 * @date 2022/04/01
 */
public class RegisterView extends JFrame {
    private final UserController userController;

    public RegisterView() {
        userController = ControllerProxy.getProxyInstance(UserControllerImpl.class);
        initComponents();
        setVisible(true);
    }

    User user = new User();

    private void manButton(ActionEvent e) {
        // TODO add your code here
        user.setGender("男");
    }

    private void girlButton(ActionEvent e) {
        // TODO add your code here
        user.setGender("女");
    }

    private void lightButton(ActionEvent e) {
        // TODO add your code here
        user.setCamp(0);
    }

    private void darkButton(ActionEvent e) {
        // TODO add your code here
        user.setCamp(1);
    }

    private void register(ActionEvent e) {
        // TODO add your code here
        String userName = userNameText.getText();
        String password = new String(this.passwordText.getPassword());
        String currentPassword = new String(currentPasswordText.getPassword());
        String emailAddress = emailText.getText();
        String phone = phoneText.getText();
        String nickName = nickNameText.getText();
        String age = ageText.getText();
        if (ValidateUtil.strIsNullOrEmpty(userName)) {
            JOptionPane.showMessageDialog(null, "用户名不能为空");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(password)) {
            JOptionPane.showMessageDialog(null, "密码不能为空！");
            return;
        }
        if (ValidateUtil.isNotPassword(password)) {
            JOptionPane.showMessageDialog(null, "密码格式错误！");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(currentPassword)) {
            JOptionPane.showMessageDialog(null, "确认密码不能为空！");
            return;
        }
        if (!password.equals(currentPassword)) {
            JOptionPane.showMessageDialog(null, "请确认密码是否一致！");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(emailAddress)) {
            JOptionPane.showMessageDialog(null, "邮箱地址不能为空！");
            return;
        }
        if (ValidateUtil.isNotEmail(emailAddress)) {
            JOptionPane.showMessageDialog(null, "邮箱地址格式错误");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(phone)) {
            JOptionPane.showMessageDialog(null, "手机号码不能为空！");
            return;
        }
        if (ValidateUtil.isNotMobile(phone)) {
            JOptionPane.showMessageDialog(null, "手机号码格式错误");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(nickName)) {
            JOptionPane.showMessageDialog(null, "昵称不能为空！");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(user.getGender())) {
            JOptionPane.showMessageDialog(null, "未选择性别！");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(age)) {
            JOptionPane.showMessageDialog(null, "年龄不能为空！");
            return;
        }
        if (ValidateUtil.isNotAge(age)) {
            JOptionPane.showMessageDialog(null, "年龄格式错误");
            return;
        }
        if (user.getCamp() == null) {
            JOptionPane.showMessageDialog(null, "未选择阵营！");
            return;
        }


        user.setAge(Integer.valueOf(age));
        user.setUserName(userName);
        user.setPassword(password);
        user.setNickName(nickName);
        user.setEmailAddress(emailAddress);
        user.setPhone(Long.valueOf(phone));
        ResultInfo<?> register = userController.register(user);
        int code = register.getCode();
        String message = register.getMessage();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            new LoginOnView();
            dispose();
        }
        if (code == REJECT_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
        }
    }

    private void back(ActionEvent e) {
        new LoginOnView();
        dispose();
    }

    private void exit(ActionEvent e) {
        // TODO add your code here
        if (JOptionPane.showConfirmDialog(null
                , "退出", "提示 ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
            dispose();
            ConnectionPoolUtil.getInstance().closeConnectionPool();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        registerButton = new JButton();
        userNameText = new JTextField();
        passwordText = new JPasswordField();
        nickNameText = new JTextField();
        manButton = new JRadioButton();
        girlButton = new JRadioButton();
        ageText = new JTextField();
        lightButton = new JRadioButton();
        darkButton = new JRadioButton();
        backButton = new JButton();
        label8 = new JLabel();
        exitButton = new JButton();
        label10 = new JLabel();
        phoneText = new JTextField();
        label11 = new JLabel();
        label12 = new JLabel();
        currentPasswordText = new JPasswordField();
        emailText = new JTextField();
        label13 = new JLabel();

        //======== this ========
        setTitle("Dream\u6ce8\u518c\u754c\u9762");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("Dream\u6ce8\u518c");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
        label1.setForeground(Color.white);
        contentPane.add(label1);
        label1.setBounds(180, 75, 180, 36);

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d\uff1a");
        label2.setForeground(Color.white);
        contentPane.add(label2);
        label2.setBounds(120, 160, label2.getPreferredSize().width, 26);

        //---- label3 ----
        label3.setText("\u5bc6  \u7801\uff1a");
        label3.setForeground(Color.white);
        contentPane.add(label3);
        label3.setBounds(125, 220, 48, 27);

        //---- label4 ----
        label4.setText("\u6635 \u79f0\uff1a");
        label4.setForeground(Color.white);
        contentPane.add(label4);
        label4.setBounds(130, 350, label4.getPreferredSize().width, 25);

        //---- label5 ----
        label5.setText("\u6027 \u522b\uff1a");
        label5.setForeground(Color.white);
        contentPane.add(label5);
        label5.setBounds(130, 385, label5.getPreferredSize().width, 26);

        //---- label6 ----
        label6.setText("\u5e74 \u9f84\uff1a");
        label6.setForeground(Color.white);
        contentPane.add(label6);
        label6.setBounds(130, 420, label6.getPreferredSize().width, 23);

        //---- label7 ----
        label7.setText("\u9635 \u8425\uff1a");
        label7.setForeground(Color.white);
        contentPane.add(label7);
        label7.setBounds(130, 450, 40, 25);

        //---- registerButton ----
        registerButton.setText("\u70b9\u4e0b\u8fd9\u4e2a\u6309\u94ae\uff0c\u4f60\u5c31\u80fd\u8fdb\u5165\u767d\u65e5\u68a6");
        registerButton.addActionListener(e -> register(e));
        contentPane.add(registerButton);
        registerButton.setBounds(145, 530, 205, 48);
        contentPane.add(userNameText);
        userNameText.setBounds(175, 160, 155, userNameText.getPreferredSize().height);
        contentPane.add(passwordText);
        passwordText.setBounds(175, 225, 160, passwordText.getPreferredSize().height);
        contentPane.add(nickNameText);
        nickNameText.setBounds(175, 350, 155, nickNameText.getPreferredSize().height);

        //---- manButton ----
        manButton.setText("\u7537");
        manButton.addActionListener(e -> manButton(e));
        contentPane.add(manButton);
        manButton.setBounds(new Rectangle(new Point(175, 390), manButton.getPreferredSize()));

        //---- girlButton ----
        girlButton.setText("\u5973");
        girlButton.addActionListener(e -> girlButton(e));
        contentPane.add(girlButton);
        girlButton.setBounds(245, 390, 51, girlButton.getPreferredSize().height);
        contentPane.add(ageText);
        ageText.setBounds(175, 420, 155, ageText.getPreferredSize().height);

        //---- lightButton ----
        lightButton.setText("\u5149\u660e");
        lightButton.addActionListener(e -> {
            lightButton(e);
        });
        contentPane.add(lightButton);
        lightButton.setBounds(new Rectangle(new Point(175, 450), lightButton.getPreferredSize()));

        //---- darkButton ----
        darkButton.setText("\u9ed1\u6697");
        darkButton.addActionListener(e -> darkButton(e));
        contentPane.add(darkButton);
        darkButton.setBounds(new Rectangle(new Point(250, 450), darkButton.getPreferredSize()));

        //---- backButton ----
        backButton.setText("\u8fd4\u56de");
        backButton.addActionListener(e -> back(e));
        contentPane.add(backButton);
        backButton.setBounds(new Rectangle(new Point(55, 70), backButton.getPreferredSize()));

        //---- label8 ----
        label8.setText("\u5bc6\u7801\u957f\u5ea6\u5e94\u4e3a8\u523020\u4f4d,\u5fc5\u987b\u5305\u542b\u5b57\u6bcd\u548c\u6570\u5b57\uff0c\u5b57\u6bcd\u533a\u5206\u5927\u5c0f\u5199\uff01");
        label8.setForeground(Color.white);
        contentPane.add(label8);
        label8.setBounds(100, 190, 423, 35);

        //---- exitButton ----
        exitButton.setText("\u9000\u51fa\u7a0b\u5e8f");
        exitButton.addActionListener(e -> exit(e));
        contentPane.add(exitButton);
        exitButton.setBounds(new Rectangle(new Point(345, 70), exitButton.getPreferredSize()));

        //---- label10 ----
        label10.setText("\u624b\u673a\u53f7\uff1a");
        label10.setForeground(Color.white);
        contentPane.add(label10);
        label10.setBounds(130, 310, 50, 35);
        contentPane.add(phoneText);
        phoneText.setBounds(175, 315, 150, phoneText.getPreferredSize().height);

        //---- label11 ----
        label11.setText("\u90ae \u7bb1\uff1a");
        label11.setForeground(Color.white);
        contentPane.add(label11);
        label11.setBounds(130, 290, 40, label11.getPreferredSize().height);

        //---- label12 ----
        label12.setText("\u786e\u8ba4\u5bc6\u7801\uff1a");
        label12.setForeground(Color.white);
        contentPane.add(label12);
        label12.setBounds(115, 255, 60, label12.getPreferredSize().height);
        contentPane.add(currentPasswordText);
        currentPasswordText.setBounds(175, 255, 155, currentPasswordText.getPreferredSize().height);
        contentPane.add(emailText);
        emailText.setBounds(175, 285, 150, emailText.getPreferredSize().height);

        //---- label13 ----
        label13.setIcon(new ImageIcon(RegisterView.class.getClassLoader().getResource("register.png")));
        contentPane.add(label13);
        label13.setBounds(-65, 0, 590, 710);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for (int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //---- buttonGroup1 ----
        ButtonGroup buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(manButton);
        buttonGroup1.add(girlButton);

        //---- buttonGroup2 ----
        ButtonGroup buttonGroup2 = new ButtonGroup();
        buttonGroup2.add(lightButton);
        buttonGroup2.add(darkButton);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JButton registerButton;
    private JTextField userNameText;
    private JPasswordField passwordText;
    private JTextField nickNameText;
    private JRadioButton manButton;
    private JRadioButton girlButton;
    private JTextField ageText;
    private JRadioButton lightButton;
    private JRadioButton darkButton;
    private JButton backButton;
    private JLabel label8;
    private JButton exitButton;
    private JLabel label10;
    private JTextField phoneText;
    private JLabel label11;
    private JLabel label12;
    private JPasswordField currentPasswordText;
    private JTextField emailText;
    private JLabel label13;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
